# Copyright 2021 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================

load("//tools:defaults.bzl", "esbuild", "ts_library")
load("//tools:enumerate_tests.bzl", "enumerate_tests")
load("//tools:make_version_test_file.bzl", "make_version_test_file")

package(default_visibility = ["//visibility:public"])

TEST_SRCS = [
    "**/*_test.ts",
    "test_node.ts",
    "test_util.ts",
]

TEST_ENTRYPOINTS = [
    "test_node.ts",
    "setup_test.ts",
]

make_version_test_file(
    name = "version_test",
    package_json = "//tfjs-core:package.json",
    version_name = "version_wasm",
)

filegroup(
    name = "test_srcs",
    srcs = glob(
        ["**/*_test.ts"],
        exclude = TEST_ENTRYPOINTS,
    ) + [
        ":version_test",
    ],
)

# Generates the 'tests.ts' file that imports all test entrypoints.
enumerate_tests(
    name = "tests",
    srcs = [":test_srcs"],
    root_path = "tfjs-backend-wasm/src",
)

ts_library(
    name = "tfjs-backend-wasm_src_lib",
    srcs = glob(
        ["**/*.ts"],
        exclude = TEST_SRCS + ["index.ts"],
    ),
    module_name = "@tensorflow/tfjs-backend-wasm/dist",
    deps = [
        "//tfjs-backend-cpu/src:tfjs-backend-cpu_lib",
        "//tfjs-backend-cpu/src:tfjs-backend-cpu_src_lib",
        "//tfjs-backend-wasm/wasm-out",
        "//tfjs-core/src:tfjs-core_lib",
        "@npm//@types/seedrandom",
        "@npm//seedrandom",
    ],
)

ts_library(
    name = "tfjs-backend-wasm_lib",
    srcs = ["index.ts"],
    module_name = "@tensorflow/tfjs-backend-wasm",
    deps = [
        ":tfjs-backend-wasm_src_lib",
    ],
)

ts_library(
    name = "tfjs-backend-wasm_test_lib",
    testonly = True,
    srcs = glob(
        TEST_SRCS,
    ) + [
        ":tests",
        ":version_test",
    ],
    module_name = "@tensorflow/tfjs-backend-wasm/dist",
    deps = [
        ":tfjs-backend-wasm_lib",
        ":tfjs-backend-wasm_src_lib",
        "//tfjs-backend-wasm/wasm-out",
        "//tfjs-core/src:tfjs-core_lib",
        "//tfjs-core/src:tfjs-core_src_lib",
        "//tfjs-core/src:tfjs-core_test_lib",
        "@npm//@types/jasmine",
        "@npm//core-js",
        "@npm//jasmine",
    ],
)

esbuild(
    name = "tfjs-backend-wasm_test_bundle",
    testonly = True,
    entry_point = "setup_test.ts",
    external = [
        "@tensorflow/tfjs",
        "worker_threads",
        "util",
        "fs",
        "path",
        "perf_hooks",
        "os",
    ],
    sources_content = True,
    target = "es6",
    deps = [
        ":tfjs-backend-wasm_lib",
        ":tfjs-backend-wasm_test_lib",
    ],
)
